package br.com.advance.automacao.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "ERRO")
@NamedQueries({@NamedQuery(name=Erro.BUSCAR_ERRO_POR_CLP_CODIGO, query="select e from Erro e where e.codigo = :codigo and e.clp.id = :idCLP"),
	@NamedQuery(name=Erro.BUSCAR_ERRO_POR_CLP, query="select e from Erro e join fetch e.clp clp where e.clp.id = :idCLP"),
	@NamedQuery(name=Erro.BUSCAR_ERRO_POR_ID, query="select e from Erro e join fetch e.clp clp where e.id = :id"),
	@NamedQuery(name=Erro.BUSCAR_ERRO_POR_CODIGO_CLP, query="select e from Erro e join fetch e.clp clp where e.clp.id = :idCLP and e.codigo = :codigo")})
public class Erro {

	public static final String BUSCAR_ERRO_POR_CLP_CODIGO = "Erro.BUSCAR_ERRO_POR_CLP_CODIGO";
	public static final String BUSCAR_ERRO_POR_CLP = "Erro.BUSCAR_ERRO_POR_CLP";
	public static final String BUSCAR_ERRO_POR_CODIGO_CLP = "Erro.BUSCAR_ERRO_POR_CODIGO_CLP";
	public static final String BUSCAR_ERRO_POR_ID = "Erro.BUSCAR_ERRO_POR_ID";

	@Id
    @Column(name = "ID")
	@GeneratedValue
	private Long id;

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="ID_CLP", referencedColumnName="ID")
	private CLP clp;

	@Column(name = "CODIGO", nullable=false, length=20, columnDefinition="VARCHAR")
	private String codigo;

	@Column(name = "DESCRICAO", nullable=false, length=500, columnDefinition="VARCHAR")
	private String descricao;

	@Column(name = "ATIVO", nullable=false, columnDefinition="BIT")
	private Boolean ativo;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public CLP getClp() {
		return clp;
	}

	public void setClp(CLP clp) {
		this.clp = clp;
	}

	public String getCodigo() {
		return codigo;
	}

	public void setCodigo(String codigo) {
		this.codigo = codigo;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		if(descricao != null && descricao.length() > 500) {
			descricao = descricao.substring(0, 500);
		}
		this.descricao = descricao;
	}

	public Boolean getAtivo() {
		return ativo;
	}

	public void setAtivo(Boolean ativo) {
		this.ativo = ativo;
	}

}
